import 'dart:io';
import 'package:flutter/material.dart';
import '../demo/ui_demo.dart';
import '../wonders/utils/device_utils.dart';
import 'package:connectivity/connectivity.dart';

class MyDemo extends StatelessWidget implements Demo {
  MyDemo(this.title);
  @override
  String title;
  @override
  Widget build(BuildContext context) {
    return new HomePage(
      title: this.title,
    );
  }
}

class HomePage extends StatelessWidget {
  String title;
  HomePage({this.title});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
        actions: <Widget>[
          IconSource("local","25"),
        ],
      ),
      body: LocalEnvDemo(),
    );
  }
}

class LocalEnvDemo extends StatefulWidget {
  @override
  _LocalEnvDemoState createState() => _LocalEnvDemoState();
}

class _LocalEnvDemoState extends State<LocalEnvDemo> {
  String _devInfo = "";
  String _netInfo = "";
  @override
  Widget build(BuildContext context) {
    return ListView(
      children: <Widget>[
        RaisedButton(
            child: Text("打开网络设置"),
            onPressed: () {
              DeviceUtils.openWIFISettings();
            }),
        RaisedButton(
            child: Text("打开位置设置"),
            onPressed: () {
              DeviceUtils.openLocationSettings();
            }),
        RaisedButton(
            child: Text("打开APP设置"),
            onPressed: () {
              DeviceUtils.openAppSettings();
            }),
        RaisedButton(
            child: Text("打开安全设置"),
            onPressed: () {
              DeviceUtils.openSecuritySettings();
            }),
        RaisedButton(
            child: Text("机器信息"),
            onPressed: () {
              getDeviceInfo();
            }),
        RaisedButton(
            child: Text("网络状态"),
            onPressed: () {
              getNetInfo();
            }),
        ListTile(
          title: Text("机器信息："),
          subtitle: Text("$_devInfo"),
        ),
        ListTile(
          title: Text("网络信息："),
          subtitle: Text("$_netInfo"),
        ),
      ],
    );
  }

  getDeviceInfo() async {
    List<String> infos = [];
    infos.add("Platform.isAndroid->${DeviceUtils.isAndroid}");
    infos.add("Platform.isIOS->${DeviceUtils.isIos}");

    DeviceUtils.deviceInfo.then((info) {
      if (DeviceUtils.isAndroid) {
        infos.add(
            'model-> ${info.androidDeviceInfo.model}'); // e.g. "Moto G (4)"
        infos.add('id-> ${info.androidDeviceInfo.id}'); // e.g. "Moto G (4)"
        infos.add('type-> ${info.androidDeviceInfo.type}'); // e.g. "Moto G (4)"
        infos.add(
            'androidId->${info.androidDeviceInfo.androidId}'); // e.g. "Moto G (4)"
        infos.add(
            'board-> ${info.androidDeviceInfo.board}'); // e.g. "Moto G (4)"
        infos.add(
            'bootloader-> ${info.androidDeviceInfo.bootloader}'); // e.g. "Moto G (4)"
        infos.add(
            'brand-> ${info.androidDeviceInfo.brand}'); // e.g. "Moto G (4)"
        infos.add(
            'device-> ${info.androidDeviceInfo.device}'); // e.g. "Moto G (4)"
        infos.add(
            'display-> ${info.androidDeviceInfo.display}'); // e.g. "Moto G (4)"
//      infos.add(
//          'fingerinfos.add-> ${info.androidDeviceInfo.fingerinfos.add}'); // e.g. "Moto G (4)"
        infos.add(
            'hardware-> ${info.androidDeviceInfo.hardware}'); // e.g. "Moto G (4)"
        infos.add(
            'version-> ${info.androidDeviceInfo.version}'); // e.g. "Moto G (4)"
        infos.add('host-> ${info.androidDeviceInfo.host}'); // e.g. "Moto G (4)"
        infos.add(
            'isPhysicalDevice-> ${info.androidDeviceInfo.isPhysicalDevice}'); // e.g. "Moto G (4)"
        infos.add(
            'manufacturer-> ${info.androidDeviceInfo.manufacturer}'); // e.g. "Moto G (4)"
        infos.add(
            'product-> ${info.androidDeviceInfo.product}'); // e.g. "Moto G (4)"
        infos.add('tags-> ${info.androidDeviceInfo.tags}'); // e.g. "Moto G (4)"
        infos.add(
            'supported32BitAbis-> ${info.androidDeviceInfo.supported32BitAbis}'); // e.g. "Moto G (4)"
        infos.add(
            'supported64BitAbis-> ${info.androidDeviceInfo.supported64BitAbis}'); // e.g. "Moto G (4)"
        infos.add(
            'manufacturer-> ${info.androidDeviceInfo.manufacturer}'); // e.g. "Moto G (4)"
        infos.add(
            'fingerprint-> ${info.androidDeviceInfo.fingerprint}'); // e.g. "Moto G (4)"
      }
      if (DeviceUtils.isIos) {
        infos.add("isPhysicalDevice->${info.iosDeviceInfo.isPhysicalDevice}");
        infos.add("isPhysicalDevice->${info.iosDeviceInfo.model}");
        infos.add("isPhysicalDevice->${info.iosDeviceInfo.name}");
        infos
            .add("isPhysicalDevice->${info.iosDeviceInfo.identifierForVendor}");
        infos.add("isPhysicalDevice->${info.iosDeviceInfo.localizedModel}");
        infos.add("isPhysicalDevice->${info.iosDeviceInfo.systemName}");
        infos.add("isPhysicalDevice->${info.iosDeviceInfo.systemVersion}");
        infos.add("isPhysicalDevice->${info.iosDeviceInfo.utsname}");
      }
      setState(() {
        _devInfo='';
        infos.forEach((value) {
          _devInfo = _devInfo + value + "\n";
        });
        print(_devInfo);
      });
    });
  }

  getNetInfo() {
    DeviceUtils.checkConnectivity().then((result) {
      List<String> infos = [];
      if (result == ConnectivityResult.mobile) {
        infos.add("I am connected to a mobile network.");
      } else if (result == ConnectivityResult.wifi) {
        infos.add("I am connected to a wifi network.");
      } else {
        infos.add("I am not  connected to a  network.");
      }
      setState(() {
        _netInfo = infos.toString();
      });
    });
  }
}
